package org.afun.csms_android;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import org.afun.csms_android.domain.User;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

public class UserAlterActivity extends AppCompatActivity {
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_alter);
        mContext=this;

        Intent intent=getIntent();
        int id=intent.getIntExtra("userId",0);
        String account=intent.getStringExtra("userAccount");
        String userName=intent.getStringExtra("userName");
        String userPwd=intent.getStringExtra("userPwd");
        String userRole=intent.getStringExtra("userRole");
        String userGender=intent.getStringExtra("userGender");

        TextView userAccount=findViewById(R.id.userAccountAlter);
        EditText userPwdAlter=findViewById(R.id.userPasswordAlter);
        EditText userNameAlter=findViewById(R.id.userNameAlter);
        TextView userRoleAlter=findViewById(R.id.UserRoleAlter);
        RadioGroup genderGroup=findViewById(R.id.genderGroupAlter);

        Button alterUserOk=findViewById(R.id.alterUserOk);
        Button cancelAlter=findViewById(R.id.cancelAlter);

        userAccount.setText(account);
        userPwdAlter.setText(userPwd);
        userNameAlter.setText(userName);
        userRoleAlter.setText(userRole);
        if(userGender.equals("男")){
            genderGroup.check(R.id.maleAlter);
        }else if(userGender.equals("女")) {
            genderGroup.check(R.id.femaleAlter);
        }

        alterUserOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password=userPwdAlter.getText().toString();
                String username=userNameAlter.getText().toString();
                RadioButton radioSelectGender=findViewById(genderGroup.getCheckedRadioButtonId());
                String gender=radioSelectGender.getText().toString();

                User user = new User();
                user.setId(id);
                user.setPassword(password);
                user.setName(username);
                user.setGender(gender);

                MyTableOperator myTableOperator=new MyTableOperator(mContext);
                TableOperator tableOperator = myTableOperator.getTableOperator();


                new android.app.AlertDialog.Builder(mContext).setTitle("提示")
                        .setMessage("你是否确定修改？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                boolean b = tableOperator.updateUserById(user);

                                if(b){
                                    new AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("修改成功！")
                                            .setPositiveButton("确定",null)
                                            .create()
                                            .show();
                                }else{
                                    new AlertDialog.Builder(mContext)
                                            .setTitle("提示信息")
                                            .setMessage("修改失败...")
                                            .setPositiveButton("确定",null)
                                            .create()
                                            .show();
                                }
                            }
                        })
                        .setNegativeButton("否",null)
                        .show();

//                Toast.makeText(v.getContext(), "you clicked deleteBtn " + user.getId(), Toast.LENGTH_SHORT).show();
            }

        });

        cancelAlter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}